﻿# 导入数据分析包：numpy（科学计算）、pandas（处理数据框）和 matplotlib/seaborn(可视化)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import wordcloud
import warnings
warnings.filterwarnings("ignore")
# 导入数据
hist_worldcup = pd.read_csv('WorldCupsSummary.csv')
matches = pd.read_csv('WorldCupMatches.csv')
print(hist_worldcup)
print(matches)
#数据清洗
# 统一"Germany FR"和"Germany"
hist_worldcup = hist_worldcup.replace(['Germany FR'], 'Germany')
# 查看数据表类型，进行必要的类型转化
# hist_worldcup.dtypes

# 将字符串类型转化为整形
# hist_worldcup['GoalsScored']= hist_worldcup["GoalsScored"].astype(int)
# hist_worldcup['QualifiedTeams']= hist_worldcup["QualifiedTeams"].astype(int)
# hist_worldcup['MatchesPlayed']= hist_worldcup["MatchesPlayed"].astype(int)
# hist_worldcup['Attendance']= hist_worldcup["Attendance"].astype(int)
print(hist_worldcup)

# 统一“联邦德国”和“德国”
matches = matches.replace(['Germany FR'], 'Germany')

# 类型转化
matches['Home Team Goals'] = matches['Home Team Goals'].astype(int)
matches['Away Team Goals'] = matches['Away Team Goals'].astype(int)

# 格式化比赛结果，如 3-2
matches['result'] = matches['Home Team Goals'].astype(str) + "-" + matches['Away Team Goals'].astype(str)
print(matches)



# 完成基础的数据预处理工作之后，我们来分析如下问题：
#
# 历年现场观众人数变化趋势
# 参赛队伍数变化趋势
# 历年进球数变化趋势
# 历史上夺冠次数最多的国家队是哪支？
# 夺冠队伍所在洲分析
# 哪些国家队能经常打入决赛/半决赛？
# 进入决赛的队伍夺冠概率是多少？
# 东道主（主办国）进入决赛/半决赛大吗？
# ######################历年现场观众人数变化趋势###########################
# 设置全局绘图参数
font = {
    'weight': 'normal',
    'size': '20'
}  # 粗细、大小

plt.rc('font', **font)

fig, ax = plt.subplots(figsize=(12, 8))
plt.title('Attendance Number')
hist_worldcup.plot.scatter(x='Attendance', y='Year', ax=ax, zorder=2, s=100)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.set_ylabel(None)
ax.set_xlabel(None)
ax.grid(visible=True)
ax.tick_params(axis='both', which='major', labelsize=15)
ax.set_yticks(hist_worldcup['Year'].tolist())
ax.set_xticks([500000, 1000000, 1500000, 2000000, 2500000, 3000000, 3500000, 4000000])
ax.ticklabel_format(style='plain')
plt.tick_params(bottom=False, left=False)
plt.show()

# ###################################参赛队伍数变化趋势######################################

fig, ax = plt.subplots(figsize=(12, 8))
plt.title('QualifiedTeams Numbers')
hist_worldcup.plot.scatter(x='QualifiedTeams', y='Year', ax=ax, zorder=2, s=100)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.set_ylabel(None)
ax.set_xlabel(None)
ax.grid(visible=True)
ax.tick_params(axis='both', which='major', labelsize=15)
ax.set_yticks(hist_worldcup['Year'].tolist())
ax.set_xticks([0, 16, 24, 32, 48])
plt.tick_params(bottom=False, left=False)
plt.show()

################################历年进球数变化趋势#################################################
fig, ax = plt.subplots(figsize=(12, 8))
plt.title('Goals Number')
hist_worldcup.plot.scatter(x='GoalsScored', y='Year', ax=ax, zorder=2, s=100)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.set_ylabel(None)
ax.set_xlabel(None)
ax.grid(visible=True)
ax.tick_params(axis='both', which='major', labelsize=15)
ax.set_yticks(hist_worldcup['Year'].tolist())
ax.set_xticks([50, 75, 100, 125, 150, 175, 200])
plt.tick_params(bottom=False, left=False)
plt.show()
######################################历史上夺冠次数最多的国家队是哪支？#############################
palette = ['yellow', 'red', 'red', 'blue', 'purple', 'coral', 'coral', 'purple']
fig, ax = plt.subplots(figsize=(16, 8))

plt.title('Champion Number Statistic')
sns.countplot(x=hist_worldcup['Winner'], palette=palette, linewidth=2.5, edgecolor=".2")
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.set_ylabel(None)
ax.set_xlabel(None)
plt.tick_params(labelleft=False, left=False, labelsize=14)

for i in ax.containers:
    plt.bar_label(i, fontsize=15)
plt.show()
####################################半决赛（4强）队伍次数统计##########################################
countries = hist_worldcup[['Winner', 'Second', 'Third', 'Fourth']].apply(pd.value_counts).reset_index().fillna(0)
countries['SemiFinal'] = countries['Winner'] + countries['Second'] + countries['Third'] + countries['Fourth']
countries['Final'] = countries['Winner'] + countries['Second']
print(countries)

# 设置颜色
clrs = [
    'blue' if (i >= 8) else 'y' if (5 <= i < 8) else 'purple' if (3 <= i < 5) else 'orangered' if (i == 2) else 'red'
    for i in countries['SemiFinal']]

fig, ax = plt.subplots(figsize=(20, 8))
plt.title('SemiFinal Statistic')
sns.barplot(data=countries, x='index', y='SemiFinal', palette=clrs, linewidth=2.5, edgecolor=".2")
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.set_ylabel(None)
ax.set_xlabel(None)
plt.tick_params(labelleft=False, left=False, labelsize=14)
plt.xticks(rotation=45)
for i in ax.containers:
    ax.bar_label(i, fontsize=15);
plt.show()
############################################决赛队伍次数统计#####################################
# 去掉没进入过决赛的队伍：
finalist = countries.drop(countries[(countries['Winner'] == 0) & (countries['Second'] == 0)].index)
# print(finalist)
clrs = ['blue' if (i >= 6) else 'y' if (i == 5) else 'yellow' if (3 <= i < 5) else 'purple' if (i == 2) else 'red' for i
        in finalist['Final']]

fig, ax = plt.subplots(figsize=(20, 8))
plt.title('Final Statistic')
sns.barplot(data=finalist, x='index', y='Final', palette=clrs, linewidth=2.5, edgecolor=".2")
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.set_ylabel(None)
ax.set_xlabel(None)
plt.tick_params(labelleft=False, left=False, labelsize=14)
plt.xticks(rotation=45)
for i in ax.containers:
    ax.bar_label(i, fontsize=15);
plt.show()
##########################################进入决赛后夺冠以来分析¶#########################################
# 选择进入决赛的队伍
ratios = np.round(finalist[(finalist['Second'] > 0) | (finalist['Winner'] > 0)], decimals=2)
print(ratios)
ratios['champion_prob'] = (ratios['Winner'] / ratios['Final'])
print(ratios)
# Set the color
clrs = [
    'blue' if (i == 1) else 'y' if (0.5 < i < 1) else 'purple' if (i == 0.5) else 'yellow' if (0 < i < 0.5) else 'red'
    for i in ratios['champion_prob']]

fig, ax = plt.subplots(figsize=(20, 8))
plt.title('Percentage of winning reaching the final')
sns.barplot(data=ratios, x='index', y='champion_prob', palette=clrs, linewidth=2.5, edgecolor=".2")
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.set_ylabel(None)
ax.set_xlabel(None)
plt.tick_params(labelleft=False, left=False, labelsize=14)
plt.xticks(rotation=45)
for i in ax.containers:
    ax.bar_label(i, fontsize=15);
plt.show()
###################################################夺冠队伍所在大洲分布##############################################
index1 = hist_worldcup['WinnerContinent'].value_counts().index.tolist()
value1 = hist_worldcup['WinnerContinent'].value_counts().values.tolist()

palette = ['yellow', 'blue']

fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(16, 8))

sns.countplot(ax=ax[0], x=hist_worldcup['WinnerContinent'], palette=palette, linewidth=2.5, edgecolor=".2")
ax[0].set_title('Champion Continent Numbers')
ax[0].spines['right'].set_visible(False)
ax[0].spines['top'].set_visible(False)
ax[0].spines['left'].set_visible(False)
ax[0].spines['bottom'].set_visible(False)
ax[0].set_ylabel(None)
ax[0].set_xlabel(None)
ax[0].tick_params(labelleft=False, left=False, labelsize=14)

for i in ax[0].containers:
    ax[0].bar_label(i, fontsize=15);

plt.pie(value1, labels=index1, autopct='%.0f%%', colors=['blue', 'yellow'],
        wedgeprops={"edgecolor": "0", 'linewidth': 2.5,
                    'antialiased': True}, startangle=90, textprops={'fontsize': 20})
ax[1].set_title('Champion Continent Ratios', size=20, weight='bold');
plt.show()
##########################################################东道主进入半决赛/决赛/夺冠概率统计#########################################
# 增加一列判断东道主（主办国）是否进入半决赛（4强）
hist_worldcup['HostTop4'] = hist_worldcup[['Winner', 'Second', 'Third', 'Fourth']].eq(hist_worldcup['HostCountry'],
                                                                                      axis=0).any(1)
# 增加一列判断东道主（主办国）是否进入决赛
hist_worldcup['HostTop2'] = hist_worldcup[['Winner', 'Second']].eq(hist_worldcup['HostCountry'], axis=0).any(1)
# 增加一列判断东道主（主办国）是否夺冠
hist_worldcup['HostWinner'] = hist_worldcup['HostCountry'] == hist_worldcup['Winner']

# 东道主进入半决赛（4强）概率

index = hist_worldcup['HostTop4'].value_counts().index.tolist()
values = hist_worldcup['HostTop4'].value_counts().values.tolist()

palette = ['blue', 'yellow']

fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(16, 8))

sns.countplot(ax=ax[0], x=hist_worldcup['HostTop4'], palette=palette, linewidth=2.5, edgecolor=".2")
ax[0].set_title('Host in Top4')
ax[0].spines['right'].set_visible(False)
ax[0].spines['top'].set_visible(False)
ax[0].spines['left'].set_visible(False)
ax[0].spines['bottom'].set_visible(False)
ax[0].set_ylabel(None)
ax[0].set_xlabel(None)
ax[0].tick_params(labelleft=False, left=False, labelsize=14)
for i in ax[0].containers:
    ax[0].bar_label(i, fontsize=15);

plt.pie(values, labels=index, autopct='%.0f%%', colors=['yellow', 'blue'],
        wedgeprops={"edgecolor": "0", 'linewidth': 2.5,
                    'antialiased': True}, startangle=90, textprops={'fontsize': 20})
ax[1].set_title('Percentage', size=20, weight='bold')

# 东道主进入决赛概率

index = hist_worldcup['HostTop2'].value_counts().index.tolist()
values = hist_worldcup['HostTop2'].value_counts().values.tolist()

palette = ['blue', 'yellow']

fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(16, 8))

sns.countplot(ax=ax[0], x=hist_worldcup['HostTop2'], palette=palette, linewidth=2.5, edgecolor=".2")
ax[0].set_title('Host in Top2')
ax[0].spines['right'].set_visible(False)
ax[0].spines['top'].set_visible(False)
ax[0].spines['left'].set_visible(False)
ax[0].spines['bottom'].set_visible(False)
ax[0].set_ylabel(None)
ax[0].set_xlabel(None)
ax[0].tick_params(labelleft=False, left=False, labelsize=14)
for i in ax[0].containers:
    ax[0].bar_label(i, fontsize=15);

plt.pie(values, labels=index, autopct='%.0f%%', colors=['blue', 'yellow'],
        wedgeprops={"edgecolor": "0", 'linewidth': 2.5,
                    'antialiased': True}, startangle=90, textprops={'fontsize': 20})
ax[1].set_title('Percentage', size=20, weight='bold')

# 东道主夺冠概率

index = hist_worldcup['HostWinner'].value_counts().index.tolist()
value = hist_worldcup['HostWinner'].value_counts().values.tolist()

palette = ['blue', 'yellow']

fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(16, 8))

sns.countplot(ax=ax[0], x=hist_worldcup['HostWinner'], palette=palette, linewidth=2.5, edgecolor=".2")
ax[0].set_title('Champion Number', size=20, weight='bold')
ax[0].spines['right'].set_visible(False)
ax[0].spines['top'].set_visible(False)
ax[0].spines['left'].set_visible(False)
ax[0].spines['bottom'].set_visible(False)
ax[0].set_ylabel(None)
ax[0].set_xlabel(None)
ax[0].tick_params(labelleft=False, left=False, labelsize=14)
for i in ax[0].containers:
    ax[0].bar_label(i, fontsize=15);

plt.pie(value, labels=index, autopct='%.0f%%', colors=['blue', 'yellow'],
        wedgeprops={"edgecolor": "0", 'linewidth': 2.5,
                    'antialiased': True}, startangle=90, textprops={'fontsize': 20})
ax[1].set_title('Champion Probability', size=20, weight='bold')
plt.show()



# 中国队参加的比赛
China = matches[(matches['Away Team Name'] == 'China PR') | (matches['Home Team Name'] == 'China PR')]
print(China)

# 统一“联邦德国”和“德国”
matches = matches.replace(['Germany FR'], 'Germany')

# 类型转化
matches['Home Team Goals'] = matches['Home Team Goals'].astype(int)
matches['Away Team Goals'] = matches['Away Team Goals'].astype(int)

# 格式化比赛结果，如 3-2
matches['result'] = matches['Home Team Goals'].astype(str) + "-" + matches['Away Team Goals'].astype(str)
print(matches)
######################################################现场观赛人数分析###################################################################
top5_attendance = matches.sort_values(by='Attendance', ascending=False)[:5]
print(top5_attendance)

top5_attendance['VS'] = top5_attendance['Home Team Name'] + " VS " + top5_attendance['Away Team Name']

# top5_attendance['attend']= top5_attendance['Attendance'].astype(str)

plt.figure(figsize=(12, 10))

ax = sns.barplot(y=top5_attendance['VS'], x=top5_attendance['Attendance'])
sns.despine(right=True)

plt.ylabel('Teams')
plt.xlabel('Attendence Number')
plt.title('Top5 Hottest Match')

for i, s in enumerate(
        "Stadium: " + top5_attendance['Stadium'] + "\nDate: " + top5_attendance['Datetime'] + "\nAttendance: " +
        top5_attendance['Attendance'].astype(str)):
    ax.text(2000, i, s, fontsize=12, color='white')

######################################################比赛进球数分析##################################################################
###################################################单场比赛进球数最多的比赛#########################################
matches['total_goals'] = matches['Home Team Goals'] + matches['Away Team Goals']
matches['VS'] = matches['Home Team Name'] + " VS " + matches['Away Team Name']

top10_goals = matches.sort_values(by='total_goals', ascending=False)[:10]

top10_goals['VS'] = top10_goals['Home Team Name'] + " VS " + top10_goals['Away Team Name']

top10_goals['total_goals_str'] = top10_goals['total_goals'].astype(str) + " goals scored"

top10_goals['Home Team Goals'] = top10_goals['Home Team Goals'].astype(int)
top10_goals['Away Team Goals'] = top10_goals['Away Team Goals'].astype(int)

top10_goals['result'] = top10_goals['Home Team Goals'].astype(str) + "-" + top10_goals['Away Team Goals'].astype(str)

plt.figure(figsize=(12, 10))
ax = sns.barplot(y=top10_goals['VS'], x=top10_goals['total_goals'])
sns.despine(right=True)
plt.ylabel('Match')
plt.xlabel('Goals')
plt.yticks(size=10)
plt.xticks(size=10)
plt.title('Top10 Goals Match', size=20)

for i, s in enumerate("Stadium " + top10_goals['Stadium'] + ", Date: " + top10_goals['Datetime'] + "\n" +
                      top10_goals['total_goals_str'] + ", match result: " + top10_goals['result']):
    ax.text(1, i, s, fontsize=12, color='white', va='center')

###############################################################比赛分差最大的比赛#################################################################
# 取主客场进球数的绝对值
matches['difference_goals'] = pd.Series.abs(matches['Home Team Goals'] - matches['Away Team Goals'])

top10_difference = matches.sort_values(by='difference_goals', ascending=False)[:10]

top10_difference['difference_goals'] = top10_difference['difference_goals'].astype(int)

top10_difference['difference_goals_str'] = top10_difference['difference_goals'].astype(str) + " goals difference"

top10_difference['result'] = top10_difference['Home Team Goals'].astype(str) + "-" + top10_difference[
    'Away Team Goals'].astype(str)

plt.figure(figsize=(12, 10))
ax = sns.barplot(y=top10_difference['VS'], x=top10_difference['difference_goals'])
sns.despine(right=True)
plt.ylabel('Teams')
plt.xlabel('Goals')
plt.yticks(size=10)
plt.xticks(size=10)
plt.title('Top10 Biggest Difference Matches', size=20)

for i, s in enumerate(
        "Stadium " + top10_difference['Stadium'] + ", Date: " + top10_difference['Datetime'] + "\n" + "stage: " +
        top10_difference['Stage'] + ".  " +
        top10_difference['difference_goals_str'] + ", match result: " + top10_difference['result']):
    ax.text(1, i, s, fontsize=12, color='white', va='center')

# ################################################################进球数分析#######################################################################################
matches = matches.replace(['Germany FR'], 'Germany')

list_countries = matches['Home Team Name'].unique().tolist()

# 分主客队来统计：
lista_home = []
lista_away = []
for i in list_countries:
    goals_home = matches.loc[matches['Home Team Name'] == i, 'Home Team Goals'].sum()
    lista_home.append(goals_home)
    goals_away = matches.loc[matches['Away Team Name'] == i, 'Away Team Goals'].sum()
    lista_away.append(goals_away)

df = pd.DataFrame({'country': list_countries, 'total_home_goals': lista_home, 'total_away_goals': lista_away})
df['total_goals'] = df['total_home_goals'] + df['total_away_goals']
most_goals = df.sort_values(by='total_goals', ascending=False)[:10]
print(most_goals)

fig, ax = plt.subplots(figsize=(16, 8))

plt.title('Top Goals Number by Country', size=16, weight='bold')
most_goals.plot(x="country", y=["total_home_goals", "total_away_goals", "total_goals"], kind="bar", ax=ax)

ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.set_ylabel(None)
ax.set_xlabel(None)
ax.tick_params(labelleft=False, left=False, labelsize=8)
ax.legend(fontsize=10)

for i in ax.containers:
    ax.bar_label(i, fontsize=10)
# ##################################失球数分析############################################################
# finalist来自“世界杯成绩分析表”小节，表示进入决赛的队伍
finalista = finalist['index'].tolist()

# 分主、客场统计：
goalsconceded_home = []
goalsconceded_away = []
match1 = []
match2 = []
for i in finalista:
    goalsconc_home = matches.loc[matches['Home Team Name'] == i, 'Away Team Goals'].sum()
    goalsconceded_home.append(goalsconc_home)
    goalsconc_away = matches.loc[matches['Away Team Name'] == i, 'Home Team Goals'].sum()
    goalsconceded_away.append(goalsconc_away)
    counted1 = (matches['Home Team Name'] == i).sum()
    counted2 = (matches['Away Team Name'] == i).sum()

    match1.append(int(counted1))
    match2.append(int(counted2))

# 按照失球数排序

df = pd.DataFrame(
    {'country': finalista, 'goalsconceded_home': goalsconceded_home, 'goalsconceded_away': goalsconceded_away,
     'matches_home': match1, 'matches_away': match2})
df['total_matches'] = df['matches_home'] + df['matches_away']
df['total_goalsconceded'] = df['goalsconceded_home'] + df['goalsconceded_away']
df['goalmatch_rate'] = (df['total_goalsconceded'] / df['total_matches']).round(2)
goals_conceded = df.sort_values(by='goalmatch_rate')[:10]
print(goals_conceded)

fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(20, 8))
goals_conceded.plot(x="country", y="total_goalsconceded", kind="bar", ax=ax[0])
ax[0].set_title('Total Goals Conceded by Country', size=20, weight='bold')
ax[0].spines['right'].set_visible(False)
ax[0].spines['top'].set_visible(False)
ax[0].spines['left'].set_visible(False)
ax[0].spines['bottom'].set_visible(False)
ax[0].set_ylabel(None)
ax[0].set_xlabel(None)
ax[0].tick_params(labelleft=False, left=False, labelsize=14)

for i in ax[0].containers:
    ax[0].bar_label(i, fontsize=15)

goals_conceded.plot(x="country", y="goalmatch_rate", kind="bar", ax=ax[1])

ax[1].set_title('Goals Conceded Ratio by Country', size=20, weight='bold')
ax[1].spines[['right', 'top', 'left']].set_visible(False)
ax[1].set_ylabel(None)
ax[1].set_xlabel(None)
ax[1].tick_params(labelleft=False, left=False, labelsize=14)
for i in ax[1].containers:
    ax[1].bar_label(i, fontsize=15)
plt.show()
#########################主队主办国词云分析############################
wrds = matches["Home Team Name"].value_counts().keys()
wc = wordcloud.WordCloud(scale=5, max_words=1000, colormap="rainbow").generate(" ".join(wrds))
plt.figure(figsize=(13, 14))
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
plt.title("Home Team Name - word cloud", color='b')
#########################客队词云分析################################
wrds = matches["Away Team Name"].value_counts().keys()
wc = wordcloud.WordCloud(scale=5, max_words=1000, colormap="rainbow").generate(" ".join(wrds))
plt.figure(figsize=(13, 14))
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
plt.title("Away Team Name - word cloud", color='b')
plt.show
#################################################################
players = pd.read_csv('WorldCupPlayers.csv')
print(players)
# 球员姓名
wrds = players["Player Name"].value_counts().keys()
wc = wordcloud.WordCloud(scale=5, max_words=1000, colormap="rainbow").generate(" ".join(wrds))
plt.figure(figsize=(13, 14))
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
plt.title("Player names - word cloud", color='b')
plt.show()
# 教练姓名
wrds1 = players["Coach Name"].str.split("(").str[0].value_counts().keys()

wc1 = wordcloud.WordCloud(scale=5, max_words=1000, colormap="rainbow", background_color="black").generate(
    " ".join(wrds1))
plt.figure(figsize=(13, 14))
plt.imshow(wc1, interpolation="bilinear")
plt.axis("off")
plt.title("Coach names- word cloud", color='b')
plt.show()
